﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Db4objects.Db4o;
using Db4objects.Db4o.CS;

using System.Configuration;
using System.Diagnostics;

using openxml.butter.controller;

namespace OpenXmlWebBetter
{
    public partial class PageWithData : System.Web.UI.Page
    {
        protected IObjectServer _server = null;
        protected IObjectContainer _db = null;

        protected override void OnInit(EventArgs e)
        {
            InitializeComponent();
            base.OnInit(e);
        }
        //----------------------------------------------------------------------
        private void InitializeComponent()
        {	
            this.Init += new System.EventHandler(PageInit);
            this.Unload += new System.EventHandler(PageExit);

        }
        

        protected void PageInit(object sender, System.EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Init Page");
           
            string filePath = ConfigurationManager.AppSettings["DatabaseFileName"];

            try
            {
                //ouvrir le serveur				
                _server = Db4oClientServer.OpenServer(filePath, 0);
                System.Diagnostics.Trace.WriteLine("Server ouvert");

                try
                {
                    //ouvrir la base de données
                    _db = _server.OpenClient();
                    System.Diagnostics.Trace.WriteLine("Client ouvert");

                }
                catch (Exception excp)
                {
                    System.Diagnostics.Trace.WriteLine("Pb lors de l'ouverture client : {0} ", excp.Message);
                    _server.Close();
                    _server = null;
                }
            }
            catch (Exception excp2)
            {
                System.Diagnostics.Trace.WriteLine("Impossiblie d'ouvrir le server :{0}", excp2.Message);
            }

        }
        //----------------------------------------------------------------------
        protected void PageExit(object sender, System.EventArgs e)
        {
            System.Diagnostics.Trace.WriteLine("Exit Page");

            if (_server != null)
            {
                try
                {
                    if (_db != null)
                    {
                        _db.Close();
                        _db = null;
                        System.Diagnostics.Trace.WriteLine("Client fermé");
                    }
                }
                catch (Exception excp)
                {
                    System.Diagnostics.Trace.WriteLine("Pb de fermeture client : {0}", excp.Message);
                }
                finally
                {
                    _server.Close();
                    _server = null;
                    System.Diagnostics.Trace.WriteLine("server fermé!");
                }


            }

        }
    }
}